# -*- coding:utf-8 -*-

import models, service, datetime
from . import formatMoney
from .ServiceException import ServiceException

caseTypeDisplayTable = {
    models.Case.TYPE_SELF : u'自处理',
    models.Case.TYPE_ASSESS : u'公估公司',
}

stateDisplayTable = {
    models.Case.STATE_CREATE : u'创建',
    models.Case.STATE_CHIEF_PUSH : u'段长推送',
    models.Case.STATE_BRANCH_RECEIVE : u'分公司推送',
    models.Case.STATE_BRANCH_PUSH : u'分公司接收',
    models.Case.STATE_ASSESS_RECEIVE : u'公估公司接收',
    models.Case.STATE_ASSESS_PUSH : u'公估公司推送',
    models.Case.STATE_INSURANCE_RECEIVE : u'保险公司接收',
    models.Case.STATE_FINISHED : u'完成'
}

def getCase(caseId, branchCompanyId = None):
    case = models.Case.getById(caseId)
    if not case:
        raise ServiceException(service.CASE_NOT_EXSIT, '案件不存在')
    if branchCompanyId and branchCompanyId != case.branch_company_id:
        raise ServiceException(service.CASE_BRANCH_COMPANY_NOT_MATCH, '案件不属于该分公司')
    return case

def getUnfinishedFormatCases(branchCompanyId):
    cases = models.Case.unfinishedCases(branchCompanyId)
    return formatCases(cases)

def getFinishedFormatCases(branchCompanyId):
    cases = models.Case.finishedCases(branchCompanyId)
    return formatCases(cases)

def formatCases(cases):
    for i, case in enumerate(cases):
        case.no = i + 1
        case.value = formatMoney(case.value)
        case.status = stateDisplayTable[case.status]
        case.insurance_time = str(case.insurance_time)
        case.type = caseTypeDisplayTable[case.type]
        case.stay_days = (datetime.datetime.now() - case.end_time).days
        case.end_time = str(case.end_time)
        case.assessor = case.assessor or ''
    return cases

def getStatistics(branchCompanyId = None):
    finishedSelfCasesValueSum = models.Case.getFinishedCasesValueSum(models.Case.TYPE_SELF, branchCompanyId)
    finishedAssessCasesValueSum = models.Case.getFinishedCasesValueSum(models.Case.TYPE_ASSESS, branchCompanyId)
    unfinishedSelfCasesValueSum = models.Case.getUnfinishedCasesValueSum(models.Case.TYPE_SELF, branchCompanyId)
    unfinishedAssessCasesValueSum = models.Case.getUnfinishedCasesValueSum(models.Case.TYPE_ASSESS, branchCompanyId)
    finishedSelfCasesCount = models.Case.getFinishedCasesCount(models.Case.TYPE_SELF, branchCompanyId)
    finishedAssessCasesCount = models.Case.getFinishedCasesCount(models.Case.TYPE_ASSESS, branchCompanyId)
    unfinishedSelfCasesCount = models.Case.getUnfinishedCasesCount(models.Case.TYPE_SELF, branchCompanyId)
    unfinishedAssessCasesCount = models.Case.getUnfinishedCasesCount(models.Case.TYPE_ASSESS, branchCompanyId)
    return {
        'total_count' : finishedSelfCasesCount + finishedAssessCasesCount + unfinishedSelfCasesCount + unfinishedAssessCasesCount,
        'total_value' : formatMoney(finishedSelfCasesValueSum + finishedAssessCasesValueSum + unfinishedSelfCasesValueSum + unfinishedAssessCasesValueSum),
        'total_finished_count' : finishedSelfCasesCount + finishedAssessCasesCount,
        'total_unfinished_count' : unfinishedSelfCasesCount + unfinishedAssessCasesCount,
        'assess_finished_count' : finishedAssessCasesCount ,
        'assess_unfinished_count' : unfinishedAssessCasesCount,
        'self_finished_count' : finishedSelfCasesCount,
        'self_unfinished_count' : unfinishedSelfCasesCount,
        'total_finished_value' : formatMoney(finishedSelfCasesValueSum + finishedAssessCasesValueSum),
        'total_unfinished_value' : formatMoney(unfinishedSelfCasesValueSum + unfinishedAssessCasesValueSum),
        'assess_finished_value' : formatMoney(finishedAssessCasesValueSum),
        'assess_unfinished_value' : formatMoney(unfinishedAssessCasesValueSum),
        'self_finished_value' : formatMoney(finishedSelfCasesValueSum),
        'self_unfinished_value' : formatMoney(unfinishedSelfCasesValueSum),
    }


